package com.qt.myio.ConvertStream;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author zsf
 * @Date 2023/9/27 23:20
 * @Version 1.0
 */
public class SerializableDemo {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        /**
         * 需求:
         * 将多个自定义对象序列化到文件中，但是对象的个数不确定，该如何操作呢?
         */
        Student student1 = new Student("zhangsng", 23, 10);
        Student student2 = new Student("lisi", 24, 10);
        Student student3 = new Student("wangwu", 25, 10);
        ArrayList<Student> students = new ArrayList<>();
        students.add(student1);
        students.add(student2);
        students.add(student3);

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("src/main/java/com/qt/myio/file/list.txt"));
        oos.writeObject(students);
        oos.close();

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("src/main/java/com/qt/myio/file/list.txt"));
        List<Student> list = (List<Student>) ois.readObject();
        list.forEach(System.out::println);
        ois.close();


    }


}
